﻿using System;
using System.Collections.Generic;
using System.Text;
using AllegNet;

namespace Ex01_Init
{
    class Main : Api
    {
        internal static int Fps = 0;
        internal static int FpsToShow = 0;
        
        public void WinMain()
        {                       
            //Define the screen resolution
            ScreenResolution ScreenRes = ScreenResolution.Size_320_240;

            //Define the gfx render mode
            GfxModes GfxMode = GfxModes.GfxDirectXWin;

            //Define the render engine type
            RenderingModes RenderingMode = RenderingModes.DoubleBuffering;

            //Define the screen buffer Bitmap type
            BmpType BmpTyp = BmpType.Memory;

            //Initialize AllegNet
            AllegNetInit();

            //Install Keyboard
            InstallKeyboard();

            //Install Timer
            InstallTimer();

            //Install the mouse
            InstallMouse();

            //Set the application running on background if it loose the focus
            SetDisplaySwitchMode(SwitchModes.SwitchBackground);

            //Set the render mode
            SetRenderMode(RenderingMode, BmpTyp);

            //Set the colorDepth
            SetColorDepth(CDepth.Depth_32_Bits);

            //Set the Gfx Mode
            SetGfxMode(GfxMode, ScreenRes);

            //Declare a callback delegate to use with timer
            Cproc cTimerFct = new Cproc(TimerFct);

            //Install cTimerFct to be calledback each 1000 ms (1 second)
            InstallInt(cTimerFct, 1000);

            //Initialize main loop condition
            bool Done = false;


            //Main Loop
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);

                //Clear the buffer
                ClearBitmap(BufferedScreen);

                //Write something to the screen
                TextOutEx(BufferedScreen, DefaultFont, "Allegnet Init example", 0, 50, MakeCol(125, 204, 255), -1);

                //Write Fps to the screen
                TextOutEx(BufferedScreen, DefaultFont, FpsToShow.ToString() + " Fps", 0, 0, MakeCol(125, 204, 255), -1);

                //Show the mouse to the screen
                ShowMouse(BufferedScreen);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Esc])
                    Done = true;
                Fps++;
            }
            //Exit AllegNet
            UninstallAllegNet();
        }

        /// <summary>
        /// Timer function
        /// </summary>
        void TimerFct()
        {
            FpsToShow = Fps;
            Fps = 0;
        }
    }
}
